<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/10/17
 * Time: 16:47
 */

namespace app\index\controller;

use app\common\controller\HomeBase;
use think\Db;

header('Access-Control-Allow-Origin:*');

class Uplevel extends HomeBase
{
    /*
     * 申请升级列表
     * account_id
     * type 0未审核 1记录
     * */
    public function uplist(){
        if($this->request->isGet()){
            $data=$this->request->get();
            if($data['type']=='0'){
                $where['status']=0;
            }elseif ($data['type']=='1'){
                $where['status']=['NEQ',0];
            }else{
                return json(['Status'=>0,'Message'=>'传参错误']);
            }
            $where['account_id']=$data['account_id'];
            $where['is_user']=0;
            $list=Db::table('os_uplevel')->alias('a')->join('__USER__ b','a.account_id=b.id')->where($where)->order('add_time')->select();
            if ($list){
                return json(['Status'=>1,'Message'=>'获取成功','data'=>$list]);
            }else{
                return json(['Status'=>0,'Message'=>'出错了，请稍后重试']);
            }
        }else{
            return json(['Status'=>0,'Message'=>'请求失败']);
        }
    }
    /*
     * 申请升级
     * */
    public function uplevel(){
        if($this->request->isGet()){
            $data=$this->request->get();
            $info=Db::table('os_user')->where(['id'=>$data['id']])->field('id,username,vip_level,in_account_id')->find();
            //升级逻辑,判断是否够资格
            $list=Db::table('os_user')->where(['in_account_id'=>$info['id']])->field('username,vip_level,id')->select();
            $num=0;
            foreach ($list as $k=>$v) {
                if ($v['vip_level']>=($info['vip_level']-1)){
                    $num++;
                }
            }
            //判断此订单交给谁审核
            $is_check=Db::table('os_user')->where(['id'=>$info['in_account_id']])->field('vip_level')->find();
            if($is_check['vip_level']<=$info['vip_level']){
                $is_user=1;
            }else{
                $is_user=0;
            }
            //判断是否存在未审核订单
            $is_live=Db::table('os_uplevel')->where(['account_id'=>$info['id'],'status'=>0])->find();
            if($is_live){
                return json(['Status'=>0,'Message'=>'存在未审核的记录，请联系邀请人或管理员处理']);
            }

            $save=[
                'now_level'=>$info['vip_level'],
                'account_id'=>$info['id'],
                'status'=>0,
                'level_money'=>$info['vip_level']*10,
                'add_time'=>date('Y-m-d H:i:s', time()),
                'is_user'=>$is_user
            ];
            if ($num>=3||$info['vip_level']==0){
                $r=Db::table('os_uplevel')->insert($save);
                if ($r){
                    return json(['Status'=>1,'Message'=>'申请成功']);
                }else{
                    return json(['Status'=>0,'Message'=>'申请失败，请稍后重试']);
                }
            }else{
                return json(['Status'=>0,'Message'=>'条件不足，申请失败']);
            }

        }else{
            return json(['Status'=>1,'Message'=>'传参错误']);
        }
    }




    /*
     * 人脉列表
     * id
     * level
     * */
    public function connections(){
        if($this->request->isGet()) {
            $data = $this->request->get();
            $where['in_account_id']=$data['id'];
            if ($data['level']!='all')
                $where['vip_level']=$data['level'];
            $list=Db::table('os_user')->where($where)->field('username,id,vip_level')->select();
            if ($list){
                return json(['Status'=>1,'Message'=>'获取成功','data'=>$list]);
            }else{
                return json(['Status'=>0,'Message'=>'获取失败']);
            }
        }else{
            return json(['Status'=>0,'Message'=>'传参失败']);
        }

    }



    /*
     * 审核升级
     * account_id  id
     * */
    public function examine(){
        if($this->request->isGet()){
            $data=$this->request->get();
            $info=Db::table('os_uplevel')->where(['account_id'=>$data['account_id']])->find();
            if($data['type']=='true'){
                $info['status']=1;
            }

            $top_id=Db::table('os_user')->where(['id'=>$data['account_id']])->field('in_account_id')->find();
            $top_info=Db::table('os_user')->where(['id'=>$top_id['in_account_id']])->field('bonus')->find();
            $bonus=intval($top_info['bonus'])+intval($info['level_money']);

            $m=Db::table('os_user')->where(['id'=>$top_id['in_account_id']])->update(['bonus'=>$bonus]);
            $r=Db::table('os_uplevel')->where(['id'=>$info['id']])->update($info);
            if($m){
                $text1='主表保存成功';
                $status=true;
            }else{
                $text1='主表保存错误';
                $status=false;
            }
            if($r){
                $text2='升级表保存成功';
                if ($status) {
                    $status=1;
                }else {
                    $status = 0;
                }
            }else{
                $text2='升级表保存错误';
                $status=0;
            }
            return json(['Status'=>$status,'Message'=>$text1.$text2]);
        }else{
            return json(['Status'=>0,'Message'=>'传参失败']);
        }
    }




    /*
     * 订单列表
     * pageNo id
     * */
    public function orderlist(){
        if($this->request->isGet()) {
            $data = $this->request->get();
            $where['a.account_id']=$data['id'];
            $where['is_user']=0;
            if($data['type']==0){
                $where['a.status']=['EQ',0];
            }else{
                $where['a.status']=['NEQ',0];
            }
            $list = Db::table('os_uplevel')->alias('a')->join('__USER__ b', 'a.account_id=b.id')->where($where)->field('a.*,b.username,b.mobile,b.wechat_code')->page($data['pageNo'], 10)->select();
            if ($list) {
                return json(['Status' => 1, 'Message' => '获取订单列表成功','data'=>$list]);
            } else {
                return json(['Status' => 0, 'Message' => '获取订单列表失败']);
            }
        }else{
            return json(['Status'=>0,'Message'=>'传参失败']);
        }

    }
    //我的人脉，团队
    public function team(){
        if($this->request->isGet()) {
            $data = $this->request->get();
            $list = Db::table('os_user')->where(['in_account_id' => $data['id']])->field('username,id,vip_level')->select();
            $number=[];
            for ($j=0;$j<=16;$j++){
                $number[$j]=['level'=>$j,'user'=>0];
            }
            foreach ($list as $k => $v) {
                for ($i = 1; $i <= 16; $i++) {
                    if ($v['vip_level'] == $i) {
                        $number[$i]['user']++;
                    }
                }
            }
            $where['in_account_id']=$data['id'];
            $countnum=Db::table('os_user')->where($where)->count('id');
            $where['vip_level']=['EGT',1];
            $uponenum=Db::table('os_user')->where($where)->count('id');
            if ($list){
                return json(['Status'=>1,'Message'=>'获取成功','data'=>$number,'count'=>$countnum,'orther'=>$uponenum]);
            }else{
                return json(['Status'=>0,'Message'=>'获取失败']);
            }
        }else{
            return json(['Status'=>0,'Message'=>'传参错误']);
        }
    }
    //我的订单
    public function myorder(){
        if($this->request->isGet()) {
            $data = $this->request->get();
            $where['b.in_account_id']=$data['id'];
            if($data['type']==0){
                $where['a.status']=['eq',0];
            }else{
                $where['a.status']=['neq',0];
            }
            if($data['search_id']){
                $where['a.account_id']=$data['search_id'];
                $list = Db::table('os_uplevel')->alias('a')->join('__USER__ b', 'a.account_id=b.id')->where($where)->field('a.*,b.username,b.mobile,b.wechat_code')->select();
            }else{
                $list = Db::table('os_uplevel')->alias('a')->join('__USER__ b', 'a.account_id=b.id')->where($where)->field('a.*,b.username,b.mobile,b.wechat_code')->page($data['pageNo'], 10)->select();
            }
            if ($list) {
                return json(['Status' => 1, 'Message' => '获取订单列表成功','data'=>$list]);
            } else {
                return json(['Status' => 0, 'Message' => '获取订单列表失败']);
            }
        }else{
            return json(['Status'=>0,'Message'=>'传参失败']);
        }
    }
}